package com.nkl.page.dao;

import com.nkl.common.dao.BaseDao;
import com.nkl.page.domain.CommentReply;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

//import com.nkl.common.util.DateUtil;
//import com.nkl.common.util.StringUtil;

public class CommentReplyDao {

	public int addCommentReply(CommentReply commentReply, Connection conn){
		String sql = "INSERT INTO comment_reply(comment_reply_id,comment_id,user_id,reply_user_id,reply_content,reply_date,comment_pic,comment_reply_pid,comment_level) values(null,?,?,?,?,?,?,?,?)";
		Object[] params = new Object[] {
			commentReply.getComment_id(),
			commentReply.getUser_id(),
			commentReply.getReply_user_id(),
			commentReply.getReply_content(),
			commentReply.getReply_date(),
			commentReply.getComment_pic(),
			commentReply.getComment_reply_pid(),
			commentReply.getComment_level()

		};
		return BaseDao.executeUpdate(sql, params, conn );
	}

	public int delCommentReply(String comment_reply_id, Connection conn){
		String sql = "DELETE FROM comment_reply WHERE comment_reply_id=?";

		Object[] params = new Object[] { new Integer(comment_reply_id)};
		return BaseDao.executeUpdate(sql, params, conn);
	}

	public int delCommentReplys(String[] comment_reply_ids, Connection conn){
		StringBuilder sBuilder = new StringBuilder();
		for (int i = 0; i <comment_reply_ids.length; i++) {
			sBuilder.append("?");
			if (i !=comment_reply_ids.length-1) {
				sBuilder.append(",");
			}
		}
		String sql = "DELETE FROM comment_reply WHERE comment_reply_id IN(" +sBuilder.toString()+")";

		Object[] params = comment_reply_ids;

		return BaseDao.executeUpdate(sql, params, conn);	}

	public int updateCommentReply(CommentReply commentReply, Connection conn){
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("UPDATE comment_reply SET comment_reply_id = " + commentReply.getComment_reply() +" ");

		sBuilder.append("where comment_reply_id = " + commentReply.getComment_reply());

		Object[] params = null;
		return BaseDao.executeUpdate(sBuilder.toString(), params, conn);	}

	public CommentReply getCommentReply(CommentReply commentReply, Connection conn){
		CommentReply _commentReply =null;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT * FROM comment_reply WHERE comment_reply_id = " + commentReply.getComment_reply());

		List<Object> list = BaseDao.executeQuery(CommentReply.class.getName(), sBuilder.toString(), null, conn);
		if (list != null && list.size() > 0) {
			 _commentReply = (CommentReply)list.get(0);
		}
		return _commentReply;
	}

	public List<CommentReply> listCommentReplys(CommentReply commentReply, Connection conn){
		List<CommentReply> commentReplies = null;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT mr.*,u1.nick_name,u1.user_no,u2.nick_name reply_nick_name FROM comment_reply mr join user u1 on mr.user_id = u1.user_id join user u2 on mr.reply_user_id = u2.user_id WHERE 1=1");
		if (commentReply.getComment_id()!=0) {
			sBuilder.append(" and comment_id = " + commentReply.getComment_id());
		}
		if (commentReply.getComment_reply_pid()!=0) {
			sBuilder.append(" and comment_reply_pid = " + commentReply.getComment_reply_pid());
		}
		sBuilder.append(" order by reply_date asc");

		List<Object> list = BaseDao.executeQuery(CommentReply.class.getName(), sBuilder.toString(), null, conn);
		if (list != null && list.size() > 0) {
			commentReplies = new ArrayList<CommentReply>();
			for (Object object : list) {
				commentReplies.add((CommentReply)object);
			}
		}
		return commentReplies;
	}
}
